package com.jiandaoshou;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiandaoshou.adapter.ChoiceYinHangAdapter;
import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.BankInfo;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.DensityUtil;
import com.jiandaoshou.utils.JsonUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ChoiceYinHangKaAct extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ListView listview;
    private ChoiceYinHangAdapter adapter;
    private List<ChoiceYinHangAdapter.ChoiceBank> datas;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
             switch (msg.what){
                 case 0:
                     datas= (List<ChoiceYinHangAdapter.ChoiceBank>) msg.obj;
                     adapter = new ChoiceYinHangAdapter(ChoiceYinHangKaAct.this);
                     adapter.addAll(datas);
                     listview.setAdapter(adapter);
                     break;
             }
        }
    };
    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_choiceyinhangka;
    }

    @Override
    protected String getTitleText() {
        return "选择银行卡";
    }

    @Override
    public void init() {
        listview = $(R.id.yinhangkalist);
        listview.setOnItemClickListener(this);
        addFootView();
        datas = new ArrayList<ChoiceYinHangAdapter.ChoiceBank>();
        HashMap<String, String> params = new HashMap<String, String>();
        Http.post(APIDefine.TF_Interface_Get_GetBankDic, params, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) throws Exception {
                JSONObject object=new JSONObject(response);
                JSONArray array=object.getJSONArray("BankDicList");
                ChoiceYinHangAdapter.ChoiceBank bank;
                BankInfo info;
                for(int i=0;i<array.length();i++){
                    info = JsonUtil.parser.fromJson(array.get(i).toString(), BankInfo.class);
                    bank=new ChoiceYinHangAdapter.ChoiceBank();
                    bank.bank=info;
                    datas.add(bank);
                }
                Message msg=handler.obtainMessage();
                msg.what=0;
                msg.obj=datas;
                msg.sendToTarget();
            }
        });
    }

    private void addFootView() {
        TextView textView = new TextView(this);
        textView.setId(android.R.id.text1);
        textView.setOnClickListener(this);
        int pading = DensityUtil.dip2px(this, 5);
        textView.setPadding(pading, pading, pading, pading);
        textView.setText("添加新卡提现");
        listview.addFooterView(textView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case android.R.id.text1://footview点击
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        adapter.update(datas.get(position),position);
        Intent intent=new Intent(ChoiceYinHangKaAct.this,YinHangKaDetailAct.class);
        intent.putExtra("bankname",datas.get(position).bank.getStrBankName());
        intent.putExtra("bank_id",datas.get(position).bank.getStrID());
        setResult(1,intent);
        finish();
    }
}
